package com.example.udpmulticastdemo;


public class MediaUDPClient {
    private static final String TAG = "MediaUDPClient";
    private static final String MULTICAST_GROUP_IP = "224.0.0.221";
    private static final int MULTICAST_GROUP_PORT = 11665;
    private MulReceiverAsyncTask mMulReceiverAsyncTask;

    private static final class MediaNettyUDPClientHolder {
        private static final MediaUDPClient INSTANCE = new MediaUDPClient();
    }

    public static MediaUDPClient getInstance() {
        return MediaNettyUDPClientHolder.INSTANCE;
    }

    public MediaUDPClient() {
    }
    public void connServer() {
        if (mMulReceiverAsyncTask==null || mMulReceiverAsyncTask.isCancelled()) {
            mMulReceiverAsyncTask = new MulReceiverAsyncTask(MULTICAST_GROUP_IP, MULTICAST_GROUP_PORT);
            mMulReceiverAsyncTask.execute();
        }
    }

    /**
     * 关闭链接
     */
    public void onStop() {
        if (mMulReceiverAsyncTask!=null) {
            mMulReceiverAsyncTask.stopTask();
            mMulReceiverAsyncTask=null;
        }
    }

    /**
     * 是否还在链接中
     * @return true还在链接中，false：链接已断开。
     */
    public boolean isConnecting(){
        return mMulReceiverAsyncTask!=null && !mMulReceiverAsyncTask.isCancelled() && mMulReceiverAsyncTask.ismIntercept();
    }
}
